package com.cstz.ydshop.system.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cstz.ydshop.common.domain.Tree;
import com.cstz.ydshop.common.utils.BuildTree;
import com.cstz.ydshop.system.dao.DeptDao;
import com.cstz.ydshop.system.domain.DeptDO;
import com.cstz.ydshop.system.service.DeptService;



@Service
public class DeptServiceImpl implements DeptService {
	@Autowired
	private DeptDao sysDeptMapper;

	@Override
	public DeptDO get(Long deptId){
		return sysDeptMapper.get(deptId);
	}

	@Override
	public List<DeptDO> list(Map<String, Object> map){
		return sysDeptMapper.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return sysDeptMapper.count(map);
	}

	@Override
	public int save(DeptDO sysDept){
		return sysDeptMapper.save(sysDept);
	}

	@Override
	public int update(DeptDO sysDept){
		return sysDeptMapper.update(sysDept);
	}

	@Override
	public int remove(Long deptId){
		return sysDeptMapper.remove(deptId);
	}

	@Override
	public int batchRemove(Long[] deptIds){
		return sysDeptMapper.batchRemove(deptIds);
	}

	@Override
	public Tree<DeptDO> getTree() {
		List<Tree<DeptDO>> trees = new ArrayList<Tree<DeptDO>>();
		List<DeptDO> sysDepts = sysDeptMapper.list(new HashMap<String,Object>(16));
		for (DeptDO sysDept : sysDepts) {
			Tree<DeptDO> tree = new Tree<DeptDO>();
			tree.setId(sysDept.getDeptId().toString());
			tree.setParentId(sysDept.getParentId().toString());
			tree.setText(sysDept.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<DeptDO> t = BuildTree.build(trees);
		return t;
	}
	
	public List<DeptDO> getChildList(Long parentId){
		List<DeptDO> list = sysDeptMapper.list(new HashMap<String,Object>(16));
		
		List<DeptDO> clist = new ArrayList<DeptDO>();
		
		DeptDO rootDept = new DeptDO();
		rootDept.setDeptId(0l);
		
		Map<Long,DeptDO> deptMap = new HashMap<Long,DeptDO>();
		deptMap.put(0l, rootDept);
		
		for(DeptDO dept : list){
			deptMap.put(dept.getDeptId(), dept);
		}
		
		for(DeptDO dept : list){
			Long pid = dept.getParentId();
			DeptDO parent = deptMap.get(pid);
			parent.addChild(dept);
		}
		
		DeptDO parentDept = deptMap.get(parentId);
		if(parentDept!=null){
			this.buildChildList(parentDept, clist);
		}
		
		return clist;
	}
	
	public void buildChildList(DeptDO parentDept,List<DeptDO> clist){
		List<DeptDO> list = parentDept.getChildList();
		
		if(CollectionUtils.isNotEmpty(list)){
			clist.addAll(list);
			
			for(DeptDO child : list){
				this.buildChildList(child, clist);
			}
		}
	}

	@Override
	public boolean checkDeptHasUser(Long deptId) {
		// TODO Auto-generated method stub
		//查询部门以及此部门的下级部门
		int result = sysDeptMapper.getDeptUserNumber(deptId);
		return result==0?true:false;
	}

	@Override
	public String buildChildIds(Long parentId) {
		
		String ids = "";
		
		List<DeptDO> list = this.getChildList(parentId);
		if(CollectionUtils.isNotEmpty(list)){
			for(DeptDO child : list){
				ids = ids + child.getDeptId()+",";
			}
			
			return ids.substring(0,ids.length()-1);
		}
		return ids;
	}

}
